Skip to content

🐛 Fix nil pointer dereferences and panics across GCP provider#6942

Closed
tas50 wants to merge 1 commit intomainfrom
fix/gcp-nil-deref-crashes
Closed

🐛 Fix nil pointer dereferences and panics across GCP provider#6942
tas50 wants to merge 1 commit intomainfrom
fix/gcp-nil-deref-crashes

Conversation

@tas50
Copy link
Copy Markdown
Member

@tas50 tas50 commented Mar 16, 2026

Summary

  • Fix guaranteed nil deref in discovery.go (inverted nil check on buckets)
  • Replace log.Fatal calls with error returns in discovery.go ListRepository
  • Fix iterator nil deref in cloudrun.go (break on error instead of falling through)
  • Guard nil protobuf timestamps across cloudrun.go, redis.go, filestore.go, kms.go
  • Guard nil instance.Settings and BackupRetentionSettings in sql.go
  • Add missing error checks after CreateResource/cryptoKeyVersionToMql in kms.go
  • Guard nil CertChains in KMS attestation handling
  • Guard nil policy.InternalProto in pubsub.go IAM policy methods
  • Add bounds checks on URL parsing in common.go and gke.go
  • Guard empty PlatformIds in common.go getAssetIdentifier
  • Guard nil nested structs in dataproc.go VirtualClusterConfig and nodePoolTargetToMql

Test plan

  • go vet ./resources/ passes (verified locally)
  • go build ./... passes (verified locally)
  • Run mql shell gcp and verify basic resource queries still work
  • Verify KMS, CloudRun, SQL, Redis, Pubsub resources return data correctly

🤖 Generated with Claude Code

- discovery.go: Fix inverted nil check (buckets == nil -> buckets.Error != nil)
- discovery.go: Replace log.Fatal with error returns in ListRepository
- cloudrun.go: Break on iterator error instead of falling through to nil deref
- cloudrun.go: Guard nil timestamps, Timeout durations, and SecretKeyRef
- sql.go: Guard nil Settings and BackupRetentionSettings
- kms.go: Add missing error checks after CreateResource/cryptoKeyVersionToMql
- kms.go: Guard nil CertChains and nil timestamps
- pubsub.go: Guard nil policy.InternalProto before accessing Bindings
- common.go: Add bounds checks on URL parsing (getNetworkByUrl, getSubnetworkByUrl, getDiskIdByUrl)
- common.go: Guard empty PlatformIds in getAssetIdentifier
- common.go: Add durationSecondsToTimePtr helper for nil-safe duration conversion
- gke.go: Add bounds checks on network/subnet path splitting
- redis.go: Guard nil CreateTime on instances and clusters
- filestore.go: Guard nil CreateTime on instances
- dataproc.go: Guard nil nested structs in VirtualClusterConfig and nodePoolTargetToMql

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown

@mondoo-code-review mondoo-code-review bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prevents nil pointer panics when querying GCP resources with missing or optional fields.

}
buckets := storage.Data.GetBuckets()
if buckets == nil {
if buckets.Error != nil {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 warning — The original code checked if buckets == nil to guard against a nil buckets value. The new code if buckets.Error != nil will panic if buckets itself is nil (i.e., if GetBuckets() returns a nil plugin result). The original nil check was likely intentional — consider keeping a nil guard:

if buckets == nil || buckets.Error != nil {
    if buckets != nil {
        return nil, buckets.Error
    }
    return nil, nil
}

Or verify that GetBuckets() is guaranteed to never return a nil result struct.

@@ -1028,6 +1028,9 @@ func (g *mqlGcpProjectGkeServiceClusterNetworkConfig) network() (*mqlGcpProjectC

// Format is projects/project-1/global/networks/net-1
params := strings.Split(networkPath, "/")
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 suggestion — The bounds check len(params) < 2 only protects params[1], but params[len(params)-1] a few lines above would also need len(params) >= 1. More importantly, the path format is projects/project-1/global/networks/net-1 (5 segments), so a stricter check like len(params) < 5 would better validate the expected structure, consistent with the checks added in common.go.

@tas50 tas50 added the pending-testing Waiting on manual testing label Mar 16, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Test Results

5 477 tests  ±0   5 473 ✅ ±0   2m 13s ⏱️ +10s
  411 suites ±0       4 💤 ±0 
   31 files   ±0       0 ❌ ±0 

Results for commit eaff5e9. ± Comparison against base commit 8384ca1.

@tas50 tas50 closed this Mar 27, 2026
@github-actions github-actions bot locked and limited conversation to collaborators Mar 27, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

pending-testing Waiting on manual testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant